﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using MapControl.Misc;

namespace MapControl
{
    public class Coordinate9x9
    {
        public double NWLon;
        public double NWLat;

        public double SELon;
        public double SELat;

        private double Altitude;

        //Big BorderSquare
        public SquareCoord bigSquare;
        //Basic BorderSquare
        public SquareCoord basicSquare;
        //Medium BorderSquare
        public SquareCoord mediumSquare;
        //Imagine BorderSquare
        public SquareCoord imagineSquare;


        //Mảng 2 chiều lưu các index của bigsquare trong tọa độ 9x9
        private string[] _bigSquareArray;
        public string[] BigSquareArray
        {
            get
            {
                return _bigSquareArray;
            }
            set { _bigSquareArray = value; }
        }

        public void Init(float x1, float y1, float x2, float y2, double Alt)
        {
            try
            {
                float dX = Math.Abs(x2 - x1);
                float dY = Math.Abs(y1 - y2);

                Altitude = Alt;
                //Big Square
                RectangleF rect = new RectangleF(x1, y1, dX, dY);
                bigSquare = new BigSquareCoord(rect, 2, 5);
                bigSquare.BigSquareArray = BigSquareArray;

                //Basic Square
                rect = new RectangleF(0, 0, bigSquare.Width, bigSquare.Height);
                basicSquare = new BasicSquareCoord(rect, 10, 10, bigSquare);

                //Medium Squares
                rect = new RectangleF(0, 0, basicSquare.Width, basicSquare.Height);
                mediumSquare = new MediumSquareCoord(rect, 3, 3, basicSquare);

                //Imagine Square
                rect = new RectangleF(0, 0, mediumSquare.Width, mediumSquare.Height);
                imagineSquare = new ImagineSquareCoord(rect, 3, 3, mediumSquare);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        public Coordinate9x9()
        {
            //North West
            NWLon = (double)97;
            NWLat = (double)30;

            //South East
            SELon = (double)117;
            SELat = (double)5;
        }

        /// <summary>
        /// Draw rectangle
        /// </summary>
        /// <param name="g"></param>
        public void Draw(Graphics g)
        {
            float width = 4;
            try
            {
                if (Altitude < 4000)
                {
                    width = 2;
                }
                else if (Altitude < 5000)
                {
                    width = 1;
                }
                else if (Altitude < 6000)
                {
                    width = (float)0.5;
                }
                else if (Altitude < 7000)
                {
                    width = (float)0.25;
                }

                //Dựa vào tỉ lệ zoom để vẽ lưới 9x9
                if (Altitude < 1000)
                {
                    //Vẽ ô vuông tưởng tượng
                    imagineSquare.Draw(g, width / 4);
                }

                if (Altitude < 2000)
                {
                    //Vẽ ô vuông vừa 
                    mediumSquare.Draw(g, width / 3);
                }

                if (Altitude < 3000)
                {
                    //Vẽ ô vuông cơ bản
                    basicSquare.Draw(g, width / 2);
                }
                if (Altitude < 7000)
                {
                    //Vẽ ô vuông lớn
                    bigSquare.Draw(g, width);
                }
                else if (Altitude < 10000)
                {
                    //Vẽ ô vuông lớn
                    bigSquare.Draw(g, 1);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
            
            
            

        }

        /// <summary>
        /// Xác định tọa độ tại vị trí mouse hiện tại
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public string GetCoordinate(PointF p)
        {
            try
            {
                string resultCoord = "";
                if (bigSquare.BorderSquare.Contains(p))
                {
                    //resultCoord = string.Format("BigSquare:{0} - BasicSquare:{1} - MediumSquare:{2} - ImagineSquare:{3}", bigSquare.GetCoord(p), basicSquare.GetCoord(p), mediumSquare.GetCoord(p), imagineSquare.GetCoord(p));
                    resultCoord = string.Format("{0}{1}{2}{3}", bigSquare.GetCoord(p), basicSquare.GetCoord(p), mediumSquare.GetCoord(p), imagineSquare.GetCoord(p));
                }
                return resultCoord;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }
    }
}
